angular.module('starter.service', [])

  //认证服务
.service('authService', function ($http,$state, $q, ngAuthSettings, UserInfo) {
    var serviceBase = ngAuthSettings.apiServiceBaseUrl;
    var authServiceFactory = {};

    var _authentication = {
        isAuth: false,
        userName: ""
    };

    var _isLogin = function ()
    {
        if (!_authentication.isAuth) {
            //location.href = "/#/userLogin";
           var s= ngAuthSettings.clientId + ngAuthSettings.clientSecret;
           $state.go('userLogin');
           return true;
        }
    }

    var _login = function (loginData) {

        var authBasic = Base64_Encode(ngAuthSettings.clientId + ":" + ngAuthSettings.clientSecret);
        var headers = {
            'Content-Type': 'application/x-www-form-urlencoded',
            'Authorization': 'Basic ' + authBasic
        };
        var data = "grant_type=password&username=" + loginData.userName + "&password=" + Base64_Encode(loginData.password);

        var deferred = $q.defer();

        $http.post(serviceBase + 'Token', data, { headers: headers })
            .success(function (response) {

                localStorage.setItem('authorizationData', angular.toJson({
                    token: response.access_token,
                    userName: loginData.userName,
                    refreshToken: response.refresh_token,
                    expires_in: parseInt(response.expires_in) * 1000 + Date.parse(new Date())
                }));

                _authentication.isAuth = true;
                _authentication.userName = loginData.userName;

                deferred.resolve(response);

            }).error(function (err, status) {
                _logOut();
                deferred.reject(err);
            });

        return deferred.promise;

    };

    var _logOut = function () {

        localStorage.removeItem('authorizationData');
        _authentication.isAuth = false;
        _authentication.userName = "";

        localStorage.removeItem('user');
        UserInfo.UserName = '用户名';
        UserInfo.DisplayName = '显示名字';
        UserInfo.IsExpired = false;
        UserInfo.PicUrl ='';
        UserInfo.CheckedHsp = [];
        UserInfo.LoadTime = new Date().toDateString();
        UserInfo.Tel = "";
        UserInfo.hisCardNumber = "";
        UserInfo.idCard = "";
        UserInfo.patName = "";
    };

    var _fillAuthData = function () {

        var authData = angular.fromJson(localStorage.getItem('authorizationData'));
        if (authData) {
            _authentication.isAuth = true;
            _authentication.userName = authData.userName;
        }

        var userData = angular.fromJson(localStorage.getItem('user'));
        if (userData) {
            UserInfo.UserName = userData.UserName;
            UserInfo.DisplayName = userData.DisplayName;
            UserInfo.IsExpired = userData.IsExpired;
            UserInfo.PicUrl = userData.PicUrl;;
            UserInfo.LoadTime = userData.LoadTime;
            UserInfo.CheckedHsp = userData.CheckedHsp;
            UserInfo.Tel = userData.Tel;
            UserInfo.hisCardNumber = userData.hisCardNumber;
            UserInfo.idCard = userData.idCard;
            UserInfo.patName = userData.patName;
        }

    };

    var _refreshToken = function (refreshToken) {

        var deferred = $q.defer();

        var authBasic = Base64_Encode(ngAuthSettings.clientId + ":" + ngAuthSettings.clientSecret);
        var headers = {
            'Content-Type': 'application/x-www-form-urlencoded',
            'Authorization': 'Basic ' + authBasic
        };

        var data = "grant_type=refresh_token&refresh_token=" + refreshToken;

        localStorage.removeItem('authorizationData');

        $http.post(serviceBase + 'token', data, { headers: headers }).success(function (response) {

            localStorage.setItem('authorizationData', angular.toJson({
                token: response.access_token,
                userName: response.userName,
                refreshToken: response.refresh_token,
                expires_in: parseInt(response.expires_in) * 1000 + Date.parse(new Date())
            }));

            deferred.resolve(response);

        }).error(function (err, status) {
            _logOut();
            deferred.reject(err);
        });

        return deferred.promise;

    };

    authServiceFactory.login = _login;
    authServiceFactory.logOut = _logOut;
    authServiceFactory.fillAuthData = _fillAuthData;
    authServiceFactory.authentication = _authentication;
    authServiceFactory.refreshToken = _refreshToken;
    authServiceFactory.isLogin = _isLogin;
    return authServiceFactory;

})

    //过滤授权服务
.service('authInterceptorService', function ($q, $injector, $location, UserInfo) {

    var authInterceptorServiceFactory = {};

    var _request = function (config) {

        config.headers = config.headers || {};

        var authData = angular.fromJson(localStorage.getItem("authorizationData"));
        if (authData) {
            config.headers.Authorization = 'Bearer ' + authData.token;
        }

        return config;

    };

    var _responseError = function (rejection) {

        if (rejection.status == 401) {
            $injector.get('authService').logOut();
            location.href = "/#/userLogin";
            $location.path("userLogin");
        }
        return $q.reject(rejection);

    };

    authInterceptorServiceFactory.request = _request;
    authInterceptorServiceFactory.responseError = _responseError;

    return authInterceptorServiceFactory;
})

    //http请求
.service('httpService', function ($q, $http, ngAuthSettings, UserInfo, appWebApi, ParData) {

    var serviceBase = ngAuthSettings.apiServiceBaseUrl;
    var httpServiceFactory = {};

    var _get = function (url, config) {

        config = config || {};
        var deferred = $q.defer();
        var promise = deferred.promise;

        $http.get(serviceBase + url, config).
            then(
            function successCallback(response) {
                deferred.resolve(response);
            },
            function errorCallback(err) {
                deferred.reject(err);
            });
        promise.success = function (fn) {
            promise.then(fn);
            return promise;
        }
        promise.error = function (fn) {
            promise.then(null, fn);
            return promise;
        }

        return deferred.promise;
    }

    var _post = function (url, data, config) {

        config = config || {};

        var deferred = $q.defer();
        var promise = deferred.promise;

        $http.post(serviceBase + url, data, config).
            then(
            function successCallback(response) {
                deferred.resolve(response);
            },
            function errorCallback(err) {
                deferred.reject(err);
            });
        promise.success = function (fn) {
            promise.then(fn);
            return promise;
        }
        promise.error = function (fn) {
            promise.then(null, fn);
            return promise;
        }

        return deferred.promise;
    }

    httpServiceFactory.get = _get;
    httpServiceFactory.post = _post;
    httpServiceFactory.baseUrl = serviceBase;
    httpServiceFactory.userInfo = UserInfo;
    httpServiceFactory.appWebApi = appWebApi;
    httpServiceFactory.parData = ParData;
    httpServiceFactory.parData2 = ParData;


    return httpServiceFactory;
})

    //登录服务
.service('accountService', function ($q, $http, ngAuthSettings, UserInfo) {

    var serviceBase = ngAuthSettings.apiServiceBaseUrl;
    var accoutServiceFactory = {};

    var _user = function () {

        var deferred = $q.defer();
        var promise = deferred.promise;

        $http.get(serviceBase + 'api/account/GetAccountInfo').success(function (response) {
            UserInfo.UserName = response.account_Code;
            UserInfo.DisplayName = response.patName;
            UserInfo.PicUrl = response.picUrl||"";
            UserInfo.IsExpired = false;
            UserInfo.CheckedHsp = [];
            UserInfo.Tel = response.tel;
            UserInfo.hisCardNumber = response.hisCardNumber;
            UserInfo.idCard = response.idCard;
            UserInfo.patName = response.patName;


            //angular.forEach(response.Modality, function (modal) {
            //    UserInfo.Modality.push({ text: modal, value: modal });
            //});


            localStorage.setItem('user', angular.toJson(UserInfo));
            deferred.resolve(response);
        }).error(function (err, status) {
            deferred.reject(err);
        });

        promise.success = function (fn) {
            promise.then(fn);
            return promise;
        }

        promise.error = function (fn) {
            promise.then(null, fn);
            return promise;
        }

        return deferred.promise;
    }

    var _modality = function (reqphy) {

        var deferred = $q.defer();
        var promise = deferred.promise;

        $http.get(serviceBase + 'api/account/GetModality?reqphy=' + reqphy).success(function (response) {
            deferred.resolve(response);
        }).error(function (err, status) {
            deferred.reject(err);
        });

        promise.success = function (fn) {
            promise.then(fn);
            return promise;
        }

        promise.error = function (fn) {
            promise.then(null, fn);
            return promise;
        }

        return deferred.promise;
    }

    var _facility = function () {

        var deferred = $q.defer();
        var promise = deferred.promise;

        $http.get(serviceBase + 'api/account/GetFacilitys').success(function (response) {
            deferred.resolve(response);
        }).error(function (err, status) {
            deferred.reject(err);
        });

        promise.success = function (fn) {
            promise.then(fn);
            return promise;
        }

        promise.error = function (fn) {
            promise.then(null, fn);
            return promise;
        }

        return deferred.promise;
    }

    accoutServiceFactory.User = _user;
    accoutServiceFactory.Modality = _modality;
    accoutServiceFactory.Facility = _facility;

    return accoutServiceFactory;

})

.service('reportService', function ($q, $http, ngAuthSettings) {

    var serviceBase = ngAuthSettings.apiServiceBaseUrl;
    var reportServiceFactory = {};

    var _report = function (reportListDto) {
        return $http.post(serviceBase + 'api/report/list', reportListDto);
    }

    var _detail = function (accno, itemName) {

        var deferred = $q.defer();
        var promise = deferred.promise;

        var param = 'accessionNumber=' + accno + '&itemName=' + itemName.replace(/\+/g, '%2B');
        $http.get(serviceBase + 'api/report/detail?' + param)
        .success(function (response) {
            deferred.resolve(response);
        })
        .error(function (err) {
            deferred.reject(err);
        });

        promise.success = function (fn) {
            promise.then(fn);
            return promise;
        }

        promise.error = function (fn) {
            promise.then(null, fn);
            return promise;
        }

        return deferred.promise;
    }

    var _update = function (reportTextDto) {

        var deferred = $q.defer();
        var promise = deferred.promise;

        $http.put(serviceBase + 'api/report/update', reportTextDto)
        .success(function (response) {
            deferred.resolve(response);
        })
        .error(function (err) {
            deferred.reject(err);
        });

        promise.success = function (fn) {
            promise.then(fn);
            return promise;
        }

        promise.error = function (fn) {
            promise.then(null, fn);
            return promise;
        }

        return deferred.promise;
    }

    var _GetRange = function (days, studyDate, type) {

        var deferred = $q.defer();
        var promise = deferred.promise;

        var param = 'days=' + days + '&studyDate=' + studyDate + '&type=' + type;

        $http.get(serviceBase + 'api/report/GetRangeForAccNo?' + param)
          .success(function (response) {
              deferred.resolve(response);
          }).error(function (err) {
              deferred.reject(err);
          });

        promise.success = function (fn) {
            promise.then(fn);
            return promise;
        }

        promise.error = function (fn) {
            promise.then(null, fn);
            return promise;
        }

        return deferred.promise;
    }

    var _StartInterpretation = function (reportAuditDto) {

        var deferred = $q.defer();
        var promise = deferred.promise;

        $http.post(serviceBase + 'api/report/StartInterpretation', reportAuditDto)
        .success(function (response) {
            deferred.resolve(response);
        }).error(function (err) {
            deferred.reject(err);
        });

        promise.success = function (fn) {
            promise.then(fn);
            return promise;
        }

        promise.error = function (fn) {
            promise.then(null, fn);
            return promise;
        }

        return deferred.promise;
    }

    var _CancelReportingStep = function (reportAuditDto) {

        var deferred = $q.defer();
        var promise = deferred.promise;

        $http.post(serviceBase + 'api/report/CancelReportingStep', reportAuditDto)
        .success(function (response) {
            deferred.resolve(response);
        }).error(function (err) {
            deferred.reject(err);
        });

        promise.success = function (fn) {
            promise.then(fn);
            return promise;
        }

        promise.error = function (fn) {
            promise.then(null, fn);
            return promise;
        }

        return deferred.promise;
    }

    var _submit = function (reportAuditDto) {

        var deferred = $q.defer();
        var promise = deferred.promise;

        $http.post(serviceBase + 'api/report/submit', reportAuditDto)
        .success(function (response) {
            deferred.resolve(response);
        }).error(function (err) {
            deferred.reject(err);
        });

        promise.success = function (fn) {
            promise.then(fn);
            return promise;
        }

        promise.error = function (fn) {
            promise.then(null, fn);
            return promise;
        }

        return deferred.promise;
    }

    var _verify = function (reportAuditDto) {

        var deferred = $q.defer();
        var promise = deferred.promise;

        $http.post(serviceBase + 'api/report/verify', reportAuditDto)
        .success(function (response) {
            deferred.resolve(response);
        }).error(function (err) {
            deferred.reject(err);
        });

        promise.success = function (fn) {
            promise.then(fn);
            return promise;
        }

        promise.error = function (fn) {
            promise.then(null, fn);
            return promise;
        }

        return deferred.promise;
    }

    var _complete = function (reportAuditDto) {

        var deferred = $q.defer();
        var promise = deferred.promise;

        $http.post(serviceBase + 'api/report/complete', reportAuditDto)
        .success(function (response) {
            deferred.resolve(response);
        }).error(function (err) {
            deferred.reject(err);
        });

        promise.success = function (fn) {
            promise.then(fn);
            return promise;
        }

        promise.error = function (fn) {
            promise.then(null, fn);
            return promise;
        }

        return deferred.promise;
    }

    reportServiceFactory.Report = _report;
    reportServiceFactory.Detail = _detail;
    reportServiceFactory.Update = _update;
    reportServiceFactory.GetRange = _GetRange;
    reportServiceFactory.StartInterpretation = _StartInterpretation;
    reportServiceFactory.CancelReportingStep = _CancelReportingStep;
    reportServiceFactory.Submit = _submit;
    reportServiceFactory.Verify = _verify;
    reportServiceFactory.Complete = _complete;

    return reportServiceFactory;
})
.service('slideBoxService', function ($http, UserInfo, ngAuthSettings, reportService) {
    var serviceBase = ngAuthSettings.apiServiceBaseUrl;
    var tabServiceFactory = [];

    var fetch = function (dir, currentDate, self) {

        if (dir < 1 || dir > 2) {
            self.items = [];
            self.hasMore = false;
            return;
        }

        console.log(self.Range);

        var modality = '';
        if (self.name == '已完成') {
            modality = '%%';
        } else {
            modality = UserInfo.CheckedType;
        }

        var dto = {
            Range: self.Range,
            Repphy: UserInfo.DisplayName,
            Modality: modality,
            FacilityOid: '%' + UserInfo.CheckedHsp + '%',
            CreateTime: currentDate,
            Direction: dir
        };

        $http.post(self.url, dto).then(function (response) {
            console.log(response);
            switch (dir) {
                case 1: // 加载更多
                    self.hasMore = response.data.length != 0
                    for (var i = 0; i < response.data.length; i++) {
                        if (response.data[i].Cover.trim() == "") {
                            response.data[i].Cover = "img/wutu.png";
                        } else {
                            response.data[i].Cover = PicUrl + response.data[i].Cover + "&scaleType=4&scale=250";
                        }
                        self.items.push(response.data[i]);
                    }
                    break;
                case 2: // 下拉刷新
                    for (var j = response.data.length - 1; j >= 0; j--) {
                        if (response.data[j].Cover.trim() == "") {
                            response.data[j].Cover = "img/wutu.png";
                        } else {
                            response.data[j].Cover = PicUrl + response.data[j].Cover + "&scaleType=4&scale=250";
                        }
                        self.items.unshift(response.data[j]);
                    }
                    break;
                default: break;
            }
            // 消除数组中重复对象
            var unique = {};
            self.items.forEach(function (gpa) { unique[JSON.stringify(gpa)] = gpa });
            self.items = Object.keys(unique).map(function (u) { return JSON.parse(u) });

            console.log(self.items.length);
        }, function (err) {
            console.log(err);
        }).finally(function () {
            self.isLock = false;
            self.callback();
        })

    }

    var finish = {
        name: '已完成', hasMore: true, items: [], isLock: false, Range: '0', type: 0,
        url: serviceBase + 'api/report/list',
        loadMore: function () {
            console.log("加载更多数据...");
            $this = this;
            var date, len = $this.items.length;
            if (len <= 0) {
                date = new Date().Format("yyyy-MM-dd hh:mm:ss.S");
            } else {
                date = $this.items[len - 1].StudyDate;
            }

            if ($this.isLock) return;
            $this.isLock = true;

            if ($this.Range == '0' || len > 0 && $this.Range <= $this.items[len - 1].AccessionNumber) {
                console.log("执行if分支");
                reportService.GetRange(-7, date, 'INTERPRETATION').then(function (range) {
                    if ($this.Range == range) {
                        $this.Range = '0';
                    } else {
                        $this.Range = range;
                    }
                    fetch(1, date, $this);
                }, function (err) {
                    console.log(err);
                    $this.isLock = false;
                    $this.callback();
                })
            } else {
                console.log("执行else分支");
                fetch(1, date, $this);
            }
        },
        doRefresh: function () {
            console.log("正在刷新数据...");
            $this = this;

            if ($this.isLock) return;
            $this.isLock = true;

            if ($this.items[0] == undefined) {
                var date = new Date().Format("yyyy-MM-dd hh:mm:ss.S");
                fetch(1, date, $this);
            } else {
                var date = $this.items[0].StudyDate;
                fetch(2, date, $this);
            }
        },
        callback: function () {
            // 回调函数
        },
        find: function (accno, name) {
            $this = this;
            var len = $this.items.length;
            for (var i = 0; i < len; i++) {
                if ($this.items[i].AccessionNumber == accno && $this.items[i].ItemName == name) {
                    return $this.items[i];
                }
            }
        },
        removeItem: function (accno, name) {
            $this = this;
            var len = $this.items.length;
            for (var i = 0; i < len; i++) {
                if ($this.items[i].AccessionNumber == accno && $this.items[i].ItemName == name) {
                    $this.items.splice(i, 1);
                    return true;
                }
            }
            return false;
        },
        init: function () {
            $this = this;
            $this.items = []; $this.hasMore = true; $this.isLock = false; $this.Range = '0'; $this.type = 0;
        }
    };

    var publication = {
        name: '已发布', hasMore: true, items: [], isLock: false, Range: '0', type: 0,
        url: serviceBase + 'api/report/finish',
        loadMore: function () {
            console.log("加载更多数据...");
            $this = this;
            var date, len = $this.items.length;
            if (len <= 0) {
                date = new Date().Format("yyyy-MM-dd hh:mm:ss.S");
            } else {
                date = $this.items[len - 1].VerifyDate;
            }

            if ($this.isLock) return;
            $this.isLock = true;

            $this.Range = '0';
            fetch(1, date, $this);

            // if($this.Range == '0' || len > 0 && $this.Range <= $this.items[len-1].AccessionNumber) {
            //   console.log("执行if分支");
            //   reportService.GetRange(-7, date, 'PUBLICATION').then(function (range) {
            //     if($this.Range == range) {
            //       $this.Range = '0';
            //     } else {
            //       $this.Range = range;
            //     }
            //     fetch(1, date, $this);
            //   },function (err) {
            //     console.log(err);
            //     $this.isLock = false;
            //     $this.callback();
            //   })
            // } else {
            //   console.log("执行else分支");
            //   fetch(1, date, $this);
            // }
        },
        doRefresh: function () {
            console.log("正在刷新数据...");
            $this = this;

            if ($this.isLock) return;
            $this.isLock = true;

            if ($this.items[0] == undefined) {
                var date = new Date().Format("yyyy-MM-dd hh:mm:ss.S");
                fetch(1, date, $this);
            } else {
                var date = $this.items[0].VerifyDate;
                fetch(2, date, $this);
            }

        },
        callback: function () {
            // 回调函数
        },
        find: function (accno, name) {
            $this = this;
            var len = $this.items.length;
            for (var i = 0; i < len; i++) {
                if ($this.items[i].AccessionNumber == accno && $this.items[i].ItemName == name) {
                    return $this.items[i];
                }
            }
        },
        removeItem: function (accno, name) {
            $this = this;
            var len = $this.items.length;
            for (var i = 0; i < len; i++) {
                if ($this.items[i].AccessionNumber == accno && $this.items[i].ItemName == name) {
                    $this.items.splice(i, 1);
                    return true;
                }
            }
            return false;
        },
        init: function () {
            $this = this;
            $this.items = []; $this.hasMore = true; $this.isLock = false; $this.Range = '0'; $this.type = 0;
        }
    };

    var checked = {
        name: '已检查', hasMore: true, items: [], isLock: false, Range: '0', type: 1,
        url: serviceBase + 'api/report/checked',
        loadMore: function () {
            console.log("加载更多数据...");
            $this = this;
            var date, len = $this.items.length;
            if (len <= 0) {
                date = new Date().Format("yyyy-MM-dd hh:mm:ss.S");
            } else {
                date = $this.items[len - 1].StudyDate;
            }

            if ($this.isLock) return;
            $this.isLock = true;

            if ($this.Range == '0' || len > 0 && $this.Range <= $this.items[len - 1].AccessionNumber) {
                console.log("执行if分支");
                reportService.GetRange(-7, date, 'INTERPRETATION').then(function (range) {
                    if ($this.Range == range) {
                        $this.Range = '0';
                    } else {
                        $this.Range = range;
                    }
                    fetch(1, date, $this);
                }, function (err) {
                    console.log(err);
                    $this.isLock = false;
                    $this.callback();
                })
            } else {
                console.log("执行else分支");
                fetch(1, date, $this);
            }
        },
        doRefresh: function () {
            console.log("正在刷新数据...");
            $this = this;

            if ($this.isLock) return;
            $this.isLock = true;

            if ($this.items[0] == undefined) {
                var date = new Date().Format("yyyy-MM-dd hh:mm:ss.S");
                fetch(1, date, $this);
            } else {
                var date = $this.items[0].StudyDate;
                fetch(2, date, $this);
            }
        },
        callback: function () {
            // 回调函数
        },
        find: function (accno, name) {
            $this = this;
            var len = $this.items.length;
            for (var i = 0; i < len; i++) {
                if ($this.items[i].AccessionNumber == accno && $this.items[i].ItemName == name) {
                    return $this.items[i];
                }
            }
        },
        removeItem: function (accno, name) {
            $this = this;
            var len = $this.items.length;
            for (var i = 0; i < len; i++) {
                if ($this.items[i].AccessionNumber == accno && $this.items[i].ItemName == name) {
                    $this.items.splice(i, 1);
                    return true;
                }
            }
            return false;
        },
        init: function () {
            $this = this;
            $this.items = []; $this.hasMore = true; $this.isLock = false; $this.Range = '0'; $this.type = 1;
        }
    };

    var pending = {
        name: '待审核', hasMore: true, items: [], isLock: false, Range: '0', type: 2,
        url: serviceBase + 'api/report/pending',
        loadMore: function () {
            console.log("加载更多数据...");
            $this = this;
            var date, len = $this.items.length;
            if (len <= 0) {
                date = new Date().Format("yyyy-MM-dd hh:mm:ss.S");
            } else {
                date = $this.items[len - 1].ReportDate;
            }

            if ($this.isLock) return;
            $this.isLock = true;

            if ($this.Range == '0' || len > 0 && $this.Range <= $this.items[len - 1].AccessionNumber) {
                console.log("执行if分支");
                reportService.GetRange(-7, date, 'VERIFICATION').then(function (range) {
                    if ($this.Range == range) {
                        console.log("执行");
                        $this.Range = '0';
                    } else {
                        $this.Range = range;
                    }
                    fetch(1, date, $this);
                }, function (err) {
                    console.log(err);
                    $this.isLock = false;
                    $this.callback();
                })
            } else {
                console.log("执行else分支");
                fetch(1, date, $this);
            }
        },
        doRefresh: function () {
            console.log("正在刷新数据...");
            $this = this;

            if ($this.isLock) return;
            $this.isLock = true;

            if ($this.items[0] == undefined) {
                var date = new Date().Format("yyyy-MM-dd hh:mm:ss.S");
                fetch(1, date, $this);
            } else {
                var date = $this.items[0].ReportDate;
                fetch(2, date, $this);
            }
        },
        callback: function () {
            // 回调函数
        },
        find: function (accno, name) {
            $this = this;
            var len = $this.items.length;
            for (var i = 0; i < len; i++) {
                if ($this.items[i].AccessionNumber == accno && $this.items[i].ItemName == name) {
                    return $this.items[i];
                }
            }
        },
        removeItem: function (accno, name) {
            $this = this;
            var len = $this.items.length;
            for (var i = 0; i < len; i++) {
                if ($this.items[i].AccessionNumber == accno && $this.items[i].ItemName == name) {
                    $this.items.splice(i, 1);
                    return true;
                }
            }
            return false;
        },
        init: function () {
            $this = this;
            $this.items = []; $this.hasMore = true; $this.isLock = false; $this.Range = '0'; $this.type = 2;
        }
    };

    tabServiceFactory.push(finish);
    tabServiceFactory.push(checked);
    tabServiceFactory.push(pending);
    tabServiceFactory.push(publication);

    return tabServiceFactory;
})
//获取图像信息
.service('imageService', function ($q, $http) {
    var imageServiceFactory = {};

    var _getImages = function (accessionNumber) {

        var deferred = $q.defer();
        var promise = deferred.promise;

        $http.get('http://121.201.110.127/KrWebService/GetImageInfo?AccessionNumber=' + accessionNumber)
          .success(function (response) {
              deferred.resolve(response);
          })
          .error(function (err) {
              deferred.reject(err);
          });

        promise.success = function (fn) {
            promise.then(fn);
            return promise;
        }

        promise.error = function (fn) {
            promise.then(null, fn);
            return promise;
        }

        return deferred.promise;
    }

    imageServiceFactory.GetImages = _getImages;

    return imageServiceFactory;
})

.service("appService", function () {
    var appServiceFactory = {};

    //检查输入框是否有输入来显示取消按钮与隐藏函数
    var inputchangeFn = function ($scope, obj1, obj2) {
        //检测输入框是否输入
        $scope.inputChange = function () {
            var conChange = obj1.val();
            var undo = obj2;
            if (conChange == "") {
                undo.hide();
                $scope.focus = function () {
                    undo.hide();
                }
                $scope.blur = function () {
                    undo.hide();
                }
            } else {
                undo.show();
                $scope.focus = function () {
                    undo.show();
                }
                $scope.blur = function () {
                    undo.hide();
                }
            }
        }
        //点击取消按钮删除输入框内容
        $scope.undoFn = function () {
            obj1.val("");
            obj2.hide();
        }
    }

    var timeFn = function (s, obj) {//进入页面开始倒计时
        if (s == 0) {
            obj.removeAttr("disabled");
            obj.val("重新发送");
            s = 60;
        } else {
            obj.attr("disabled", "true");
            obj.val(s + "秒");
            s--;
            setTimeout(function () {
                timeFn(s, obj);
            }, 1000)
        }
    }

    appServiceFactory.inputchangeFn = inputchangeFn;
    appServiceFactory.timeFn = timeFn;

    return appServiceFactory;
});



